package v1

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"

	"application/model"
	"application/pkg/app"
	"application/pkg/jwt"
)

type RegisterUserBody struct {
	Username string `json:"user" form:"user"`
	Mobile   string `json:"mobile" form:"mobile" binding:"numeric,len=11"`
	Power    int    `json:"power" form:"power"`
	Password string `json:"pass" form:"pass"`
}

type UpdateUserMessageBody struct {
	Username string `json:"user" form:"user"`
	Mobile   string `json:"mobile" form:"mobile" binding:"numeric,len=11"`
	Power    int    `json:"power" form:"power"`
}

type DelUserBody struct {
	UserId int `json:"uid" form:"uid"`
}

type LogOutBody struct {
	UserId int `json:"uid" form:"uid"`
}

type GetUserMessageBody struct {
	UserId int `json:"uid" form:"uid"`
}

type LoginBody struct {
	Username string `json:"name" form:"name"`
	Mobile   string `json:"mobile" form:"mobile" binding:"numeric,len=11"`
	Password string `json:"pass" form:"pass" `
	Code     string `json:"code" form:"code"`
}

// Login 用户等录
func Login(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(LoginBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "登录失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	if body.Mobile != "" && body.Code != "" {
		res := model.TestCode(body.Mobile, body.Code)
		if !res {
			appG.Response(http.StatusBadRequest, "登录失败", "手机号验证码出错")
			return
		}
		ref := model.GetIdByMobile(body.Mobile)
		jwt.CreateToken("wanglinjun", time.Now().Unix(), 3600, int(ref))
		appG.Response(http.StatusOK, "登录成功", "")
		return
	}
	res := model.Login(body.Username, body.Password)
	if res == 0 {
		appG.Response(http.StatusBadRequest, "登录失败", "密码出错")
		return
	}
	token, _ := jwt.CreateToken("wanglinjun", time.Now().Unix(), 3600, int(res))
	appG.Response(http.StatusOK, "登录成功", token)
	return
}

// Register 用户注册
func Register(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(RegisterUserBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	err := model.AddUser(model.User{
		Username: body.Username,
		Password: body.Password,
		Power:    body.Power,
		Status:   1,
		Mobile:   body.Mobile,
	})
	if err != nil {
		appG.Response(http.StatusBadRequest, "注册用户失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	appG.Response(http.StatusOK, "注册用户成功", "")
	return
}

// UploadUser 修改用户信息
func UploadUser(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(UpdateUserMessageBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	err := model.AddUser(model.User{
		Username: body.Username,
		Power:    body.Power,
		Mobile:   body.Mobile,
	})
	if err != nil {
		appG.Response(http.StatusBadRequest, "修改用户信息失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	appG.Response(http.StatusOK, "修改用户信息成功", "")
	return
}

// UserMessage 获取用户信息
func UserMessage(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(GetUserMessageBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	res := model.GetMessage(body.UserId)
	appG.Response(http.StatusOK, "获取用户信息成功", res)
	return
}

// DelUser 删除用户信息
func DelUser(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(DelUserBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	err := model.DelUser(body.UserId)
	if err != nil {
		appG.Response(http.StatusBadRequest, "删除用户信息失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	appG.Response(http.StatusOK, "删除用户信息成功", "")
	return
}

// Logout 用户登出
func Logout(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(LogOutBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
}
